package com.lpy.book.JFrame;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginPage extends JFrame {

    public LoginPage() {
        // 设置窗口大小
        setSize(600, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("图书管理系统登录");

        // 创建面板
        JPanel panel = new JPanel() {
            // 设置背景图片
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Image image = new ImageIcon("D:\\softare\\install\\照视\\照片\\云.png").getImage();
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        };
        panel.setLayout(null);

        // 创建用户名标签
        JLabel userLabel = new JLabel("用户名:");
        userLabel.setBounds(50, 200, 100, 30);
        userLabel.setForeground(Color.WHITE);
        panel.add(userLabel);

        // 创建用户名输入框
        JTextField userText = new JTextField(20);
        userText.setBounds(150, 200, 300, 30);
        panel.add(userText);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(50, 250, 100, 30);
        passwordLabel.setForeground(Color.WHITE);
        panel.add(passwordLabel);

        // 创建密码输入框
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(150, 250, 300, 30);
        panel.add(passwordText);

        // 创建权限选择标签
        JLabel roleLabel = new JLabel("权限:");
        roleLabel.setBounds(50, 300, 100, 30);
        roleLabel.setForeground(Color.WHITE);
        panel.add(roleLabel);

        // 创建权限选择下拉框
        String[] roles = {"用户", "管理员"};
        JComboBox<String> roleComboBox = new JComboBox<>(roles);
        roleComboBox.setBounds(150, 300, 300, 30);
        panel.add(roleComboBox);

        // 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(150, 360, 150, 30);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userText.getText();
                String password = new String(passwordText.getPassword());
                String role = (String) roleComboBox.getSelectedItem();

                System.out.println("用户名: " + username);
                System.out.println("密码: " + password);
                System.out.println("权限: " + role);
            }
        });
        panel.add(loginButton);

        // 创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(300, 360, 150, 30);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打开注册对话框
                showRegisterDialog();
            }
        });
        panel.add(registerButton);

        // 添加面板到窗口
        add(panel);

        // 设置窗口可见
        setVisible(true);

        // 让窗口显示在屏幕中央
        setLocationRelativeTo(null);
    }

    private void showRegisterDialog() {
        // 创建注册对话框
        JDialog registerDialog = new JDialog(this, "用户注册", true);
        registerDialog.setSize(400, 400);
        registerDialog.setLayout(null);
        registerDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // 添加用户名标签和输入框
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setBounds(50, 50, 100, 30);
        registerDialog.add(usernameLabel);

        JTextField usernameText = new JTextField(20);
        usernameText.setBounds(150, 50, 200, 30);
        registerDialog.add(usernameText);

        // 添加密码标签和输入框
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(50, 100, 100, 30);
        registerDialog.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(150, 100, 200, 30);
        registerDialog.add(passwordText);

        // 添加手机号标签和输入框
        JLabel phoneLabel = new JLabel("手机号:");
        phoneLabel.setBounds(50, 150, 100, 30);
        registerDialog.add(phoneLabel);

        JTextField phoneText = new JTextField(15);
        phoneText.setBounds(150, 150, 200, 30);
        registerDialog.add(phoneText);

        // 添加性别标签和单选按钮
        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setBounds(50, 200, 100, 30);
        registerDialog.add(genderLabel);

        JRadioButton maleRadioButton = new JRadioButton("男");
        JRadioButton femaleRadioButton = new JRadioButton("女");
        ButtonGroup genderGroup = new ButtonGroup();
        genderGroup.add(maleRadioButton);
        genderGroup.add(femaleRadioButton);
        JPanel genderPanel = new JPanel();
        genderPanel.add(maleRadioButton);
        genderPanel.add(femaleRadioButton);
        genderPanel.setBounds(150, 200, 200, 30);
        registerDialog.add(genderPanel);

        // 添加验证码标签和输入框
        JLabel captchaLabel = new JLabel("验证码:");
        captchaLabel.setBounds(50, 250, 100, 30);
        registerDialog.add(captchaLabel);

        JTextField captchaText = new JTextField(5);
        captchaText.setBounds(150, 250, 100, 30);
        registerDialog.add(captchaText);

        JButton generateCaptchaButton = new JButton("生成验证码");
        generateCaptchaButton.setBounds(260, 250, 120, 30);
        generateCaptchaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 在实际应用中，生成复杂的验证码逻辑
                String captcha = String.valueOf((int) (Math.random() * 10000));
                JOptionPane.showMessageDialog(null, "验证码：" + captcha);
            }
        });
        registerDialog.add(generateCaptchaButton);

        // 添加注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(150, 300, 100, 30);
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 添加密码输入框监听器

                if (validatePassword(passwordText.getPassword()) == false) {

                }else {
                    // 处理注册逻辑，此处简化为输出提示信息
                    JOptionPane.showMessageDialog(null, "注册成功");
                    registerDialog.dispose(); // 关闭注册对话框
                }

            }
        });
        registerDialog.add(registerButton);

        // 居中设置
        int centerX = (int) (getLocation().getX() + getWidth() / 2 - registerDialog.getWidth() / 2);
        int centerY = (int) (getLocation().getY() + getHeight() / 2 - registerDialog.getHeight() / 2);
        registerDialog.setLocation(centerX, centerY);

        // 显示注册对话框
        registerDialog.setVisible(true);
    }

    // 添加密码校验方法
    private boolean validatePassword(char[] password) {
        String passwordStr = new String(password);

        // 判断密码是否为空
        if (passwordStr.isEmpty()) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return false;
        }

        // 使用正则表达式定义密码的格式必须为6-16位的字母和数字的组合
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        if (!passwordStr.matches(regex)) {
            JOptionPane.showMessageDialog(null, "密码格式不符合要求");
            return false;
        }

        return true;
    }

    // 添加用户名是否已存在的检查方法
    private boolean checkUsernameExist(String username) {
        // 这里应该是查询数据库的逻辑
        // 假设这里直接返回false，表示用户名不存在
        return false;
    }



    public static void main(String[] args) {
        new LoginPage();
    }
}



